<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="生成数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="num">
              <a-input-number v-model="model.num" placeholder="请输入生成数量" :min="1" :max="500" style="width:100%;" ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="题库分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <ao-category-select v-model="model.categoryId" returnColumn="code" pcode="" placeholder="请选择对应题库分类"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="owner">
              <j-select-user-by-dep v-model="model.owner" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AoCdkeyForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           num: [
              { required: true, message: '请输入生成数量!'}
           ],
           categoryId: [
             { required: true, message: '请选择题库分类!'} ,{
              validator: (rule, value, callback) => {
                if( !value ){
                  callback("请选择题库分类!")
                } else if( value.length < 9 ){
                  callback("请选择底级分类")
                } else{
                  callback()
                }
              }
            }
              // { required: true, message: '请选择题库分类!'},
           ]
        },
        url: {
          add: "/cdkey/aoCdkey/batchAdd",
          queryById: "/cdkey/aoCdkey/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            httpAction(this.url.add,this.model,'post').then((res)=>{

            }).finally(() => {
              this.$message.success("请求成功,Cdkey生成中,请等待5-10s后刷新查看，如未生成，请联系管理员!");
              this.$emit("ok");
              that.confirmLoading = false;
            })
          }
         
        })
      },
      handleCategoryChange(value,backObj){
         this.model = Object.assign(this.model, backObj);
      }
    }
  }
</script>